<?php
namespace Home\Controller;
class OrderController extends CommonController{
	
	public function pay(){
		$this->add_step('支付订单');
		// 订单号
		$order_sn   = $this->_get('order_sn', '对不起，您的操作有误！');
		// 订单信息
		$info       = D('Order')->info($order_sn);
		// 支付配置
		$pay_config = D('Config')->get('', 'pay');
		// var_dump($pay_config);exit;
		if(IS_POST){
			$order_title  = $info['title'];
			$order_amount = $info['order_amount'];
			$pay_type     = $this->_post('pay_type', '请选择支付类型！');

			// ======================= 模拟支付start =======================//
			// D('Order')->order_pay($pay_type, $order_sn, time(), $order_amount, $info['user_id'], $info['user_id'], 'system_test');
			// redirect(U('UserOrder/detail', array('order_sn' => $order_sn)));
			// =======================  模拟支付end  =======================//

			switch ($pay_type) {
				//支付宝
				case 'alipay':
					//导入支付宝包
					import("WEBAlipay", 'Public/Class/Pay/Alipay');
					$alipay = new \WEBAlipay();
					$alipay->seller_email  = $pay_config['alipay_seller_email'];
					$alipay->partner       = $pay_config['alipay_partner'];
					$alipay->key           = $pay_config['alipay_key'];
					$alipay->notify_url    = U('Alipay/notify_url');
					$alipay->return_url    = U('Alipay/call_back');
					$alipay->submit($order_sn, $order_title, $order_amount, $order_title);
					break;

				//银联支付
				case 'upmp':
					import("AppUpmpService", 'Public/Class/Pay/UPMP');
					$cert_path = D('File')->get_file_info($pay_config['upmp_web_pfx_file'], 'filepath');
					$pay  = new \AppUpmpService('web', $pay_config['upmp_web_merchant_id'], $cert_path, $pay_config['upmp_web_cert_pwd']);
					//支付成功回调地址
					$pay->notify_url = U('upmpPay/notify_url');
					$pay->return_url = U('upmpPay/call_back');

					$pay->get_web_pay_sign($order_sn, $order_title, $order_amount);
					break;

				//微信支付
				case 'weixin':
					import("WeixinPay", 'Public/Class/Pay/WxPay/');
					$weixin_pay = new \WeixinPay();
					$weixin_pay->appid      = $pay_config['weixin_web_appid'];
					$weixin_pay->mchid      = $pay_config['weixin_web_mchid'];
					$weixin_pay->key        = $pay_config['weixin_web_key'];
					$weixin_pay->appsecret  = $pay_config['weixin_web_appsecret'];
					$weixin_pay->cert_path  = D('File')->get_file_info($pay_config['weixin_cert_file'], 'filepath', true);
					$weixin_pay->key_path   = D('File')->get_file_info($pay_config['weixin_key_file'], 'filepath', true);
					$weixin_pay->notify_url = U('WeixinPay/notify_url');

					// 获取签名URL
					$code_url = $weixin_pay->get_pay_code_url($order_sn, $order_title, $order_amount);
					if(!$code_url){
						$this->error('微信签名失败！');
					}
					// $code_url = 'weixin://wxpay/bizpayurl?pr=gt493ok';
					$code_url = encode($code_url, 'pay.milv.weixin');
					$this->assign('code', $code_url);
					$this->assign('info', $info);
					$this->display('weixin_pay');
					break;


				//快钱支付-银行卡支付
				case 'kuaiqian':
					import("KuaiQianPayService",'Public/Class/Pay/KuaiQian');
					$cert_path = D('File')->get_file_info($pay_config['kuaiqian_cer_path'], 'filepath');
					$pem_path  = D('File')->get_file_info($pay_config['kuaiqian_pem_path'], 'filepath');
					$pay = new \KuaiQianPayService($cert_path, $pem_path);
					$pay->merchant_id      = $pay_config['kuaiqian_merchant_id'];
					$pay->vpos_merchant_id = $pay_config['kuaiqian_vpos_merchant_id'];
					$pay->soap_merchant_id = $pay_config['kuaiqian_soap_merchant_id'];
					$pay->terminal_id      = $pay_config['kuaiqian_terminal_id'];
					$pay->merchant_key     = $pay_config['kuaiqian_merchant_key'];
					$pay->cert_pass        = $pay_config['kuaiqian_cert_pass'];
					//支付成功跳转页面
					$callback_data   = urlencode(encode(U('UserOrder/detail', array('order_sn' => $order_sn))));
					$pay->notify_url = U('KuaiqianPay/notify_url');
					$pay->return_url = U('KuaiqianPay/notify_url', array('callback_data' => $callback_data));
					$html = $pay->get_web_pay($order_sn, $order_title, $order_amount);
					header('Content-Type:text/html;charset=utf-8');
					exit($html);
					break;

				default:
					# code...
					break;
			}
			exit;
		}
		
		$this->assign('pay_config', $pay_config);
		$this->assign('info', $info);
		$this->display();
	}
	// 微信支付二维码
	public function weixin_pay_qrcode(){
		$code    = $this->_get('code', '您的操作有误！');
		$code    = urldecode($code);
		$content = decode($code, 'pay.milv.weixin');

		import("QRcode", 'Public/Class/Other/');
		$qrcode = \QRcode::png($content, false, QR_ECLEVEL_L, 10, 0);
	}
	// 判断订单是否支付
	public function get_order_is_pay(){
		$order_sn   = $this->_get('order_sn', '您的操作有误！');
		$order_type = $this->_get('order_type');
		$where['order_sn'] = $order_sn;
		if($order_type == 'recharge'){
			$info = D('RechargeOrder')->where($where)->find();
			$is_payed = $info['status'] == 1;
		}else{
			$info = D('Order')->where($where)->find();
			$is_payed = $info['status'] == 'doing-lawyer';
		}
		if($is_payed){
			$this->success('订单已支付！');
		}else{
			$this->error('订单尚未支付！');
		}
	}
	// 支付成功跳转
	public function pay_success_jump(){
		$order_sn   = $this->_get('order_sn', '您的操作有误！');
		$order_type = $this->_get('order_type');
		if($order_type == 'recharge'){
			redirect(U('UserAccount/index'));
		}else{
			redirect(U('UserOrder/detail', array('order_sn' => $order_sn)));
		}
	}

	//账户充值
	public function recharge_pay(){
		if(IS_POST){
			$pay_type  = $this->_post('pay_type', '请选择充值方式！');
			$money     = $this->_post('money','充值金额不能为空！');
			$bank_type = $this->_post('bank_type');

			// 创建充值订单
			$info = D('Order')->submit_recharge_order($this->user_id, $pay_type, $money, 'web');
			//充值订单号
			$order_sn      = $info['order_sn'];
			//订单标题
			$order_title   = $info['order_title'];
			//订单内容
			$order_content = $info['order_content'];
			//充值金额
			$order_amount  = $info['amount'];

			// ======================= 模拟支付start =======================//
			// D('Order')->recharge_order_pay($pay_type, $order_sn, time(), $order_amount, $info['user_id'], $info['user_id'], 'system_test');
			// redirect(U('UserAccount/index'));
			// =======================  模拟支付end  =======================//

			// 支付配置
			$pay_config = D('Config')->get('', 'pay');
			switch ($pay_type) {
				//支付宝
				case 'alipay':
					//导入支付宝包
					import("WEBAlipay", '../Common/Class/Pay/Alipay/');
					$alipay = new \WEBAlipay();
					$alipay->seller_email  = $pay_config['alipay_seller_email'];
					$alipay->partner       = $pay_config['alipay_partner'];
					$alipay->key           = $pay_config['alipay_key'];
					$alipay->notify_url    = U('Alipay/notify_url', array('order_type' => 'recharge'));
					$alipay->return_url    = U('Alipay/call_back', array('order_type' => 'recharge'));
					$alipay->submit($order_sn, $order_title, $order_amount, $order_title);
					break;

				//银联支付
				case 'upmp':
					import("AppUpmpService", '../Common/Class/Pay/UPMP/');
					$cert_path = D('File')->get_file_info($pay_config['upmp_web_pfx_file'], 'filepath', true);
					$pay  = new \AppUpmpService('web', $pay_config['upmp_web_merchant_id'], $cert_path, $pay_config['upmp_web_cert_pwd']);
					//支付成功回调地址
					$pay->notify_url = U('upmpPay/notify_url', array('order_type' => 'recharge'));
					$pay->return_url = U('upmpPay/call_back', array('order_type' => 'recharge'));

					$pay->get_web_pay_sign($order_sn, $order_title, $order_amount);
					break;

				//微信支付
				case 'weixin':
					import("WeixinPay", '../Common/Class/Pay/WxPay/');
					$weixin_pay = new \WeixinPay();
					$weixin_pay->appid      = $pay_config['weixin_web_appid'];
					$weixin_pay->mchid      = $pay_config['weixin_web_mchid'];
					$weixin_pay->key        = $pay_config['weixin_web_key'];
					$weixin_pay->appsecret  = $pay_config['weixin_web_appsecret'];
					$weixin_pay->cert_path  = D('File')->get_file_info($pay_config['weixin_cert_file'], 'filepath', true);
					$weixin_pay->key_path   = D('File')->get_file_info($pay_config['weixin_key_file'], 'filepath', true);
					$weixin_pay->notify_url = U('WeixinPay/recharge_notify_url');

					// 获取签名URL
					$code_url = $weixin_pay->get_pay_code_url($order_sn, $order_title, $order_amount);
					if(!$code_url){
						$this->error('微信签名失败！');
					}
					// $code_url = 'weixin://wxpay/bizpayurl?pr=gt493ok';
					$code_url = encode($code_url, 'pay.sdz.weixin');
					$this->assign('code', $code_url);
					$this->assign('info', $info);
					$this->assign('order_sn', $order_sn);
					$this->assign('order_type', 'recharge');
					$this->display('weixin_pay');
					break;

				//快钱支付-银行卡支付
				case 'kuaiqian':
					import("KuaiQianPayService",'../Common/Class/Pay/KuaiQian/');
					$cert_path = D('File')->get_file_info($pay_config['kuaiqian_cer_path'], 'filepath', true);
					$pem_path  = D('File')->get_file_info($pay_config['kuaiqian_pem_path'], 'filepath', true);
					$pay = new \KuaiQianPayService($cert_path, $pem_path);
					$pay->merchant_id      = $pay_config['kuaiqian_merchant_id'];
					$pay->vpos_merchant_id = $pay_config['kuaiqian_vpos_merchant_id'];
					$pay->soap_merchant_id = $pay_config['kuaiqian_soap_merchant_id'];
					$pay->terminal_id      = $pay_config['kuaiqian_terminal_id'];
					$pay->merchant_key     = $pay_config['kuaiqian_merchant_key'];
					$pay->cert_pass        = $pay_config['kuaiqian_cert_pass'];
					//支付成功跳转页面
					$callback_data   = urlencode(encode(U('UserOrder/detail', array('order_sn' => $order_sn))));
					$pay->notify_url = U('KuaiqianPay/notify_url', array('order_type' => 'recharge'));
					$pay->return_url = U('KuaiqianPay/notify_url', array('order_type' => 'recharge'));
					$html = $pay->get_web_pay($order_sn, $order_title, $order_amount);
					header('Content-Type:text/html;charset=utf-8');
					exit($html);
					break;

				default:
					# code...
					break;
			}
			exit;

		}
	}
}